package com.ktjiaoyu.service.impl;
import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.mapper.SysRoleMapper;
import com.ktjiaoyu.service.SysRoleService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
 * @return 13141
 * 2025/7/3
 * 10:00
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleServiceImpl.class);
    @Resource
    private SysRoleMapper sysRoleMapper;
    // 查询角色信息列表
    @Override
    public List<SysRole> findAll() throws Exception {
        try {
            return sysRoleMapper.getAll();
        } catch (Exception e) {
            LOGGER.error("执行SysRoleServiceImpl.findAll()方法出现异常." ,e);
            throw e;
        }
    }
    // 保存或修改角色信息
    @Override
    public boolean saveOrUpdate(SysRole entity) throws Exception {
        try {
            if (entity.getId() == null) {
                // 无主键则添加
                return sysRoleMapper.insert(entity) > 0;
            } else {
                // 有主键则修改
                return sysRoleMapper.update(entity) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("执行 SysROleServiceImpl.saveOrUpdate方法出现异常", e);
            throw e;
        }
    }
    // 根据编码查询角色
    @Override
    public SysRole findByCode(String code) throws Exception {
        try {
            return sysRoleMapper.getByCode(code);
        } catch (Exception e) {
            LOGGER.error("执行 findByCode()方法出现异常", e);
            throw e;
        }
    }
    //  根据 id 查询
    @Override
    public SysRole findById(Long id) throws Exception {
        try {
            return sysRoleMapper.getById(id);
        } catch (Exception e) {
            LOGGER.error("执行 SysRoleServiceImpl" + ".findById(Long)方法出现异常.", e);
            throw e;
        }
    }
    // 删除角色
    @Override
    @Transactional
    public int delete(Long id) {
        return sysRoleMapper.delete(id);
    }
}
